﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 池化控制分部类
/// </summary>
public partial class GpuHudFacade
{
    #region 池集合(方便管理) 瓢字池,防止转型消耗 
    private Dictionary<int, GpuHudEntityPool<GpuFlyTextEntity>> flyTextPool;
    /// <summary>
    /// 瓢字池
    /// </summary>
    /// <param name="styleId"></param>
    /// <returns></returns>
    public GpuFlyTextEntity GetFlyTextEntity(int styleId)
    {
        GpuHudEntityPool<GpuFlyTextEntity> curPool = null;
        if (flyTextPool == null)
        {
            flyTextPool = new Dictionary<int, GpuHudEntityPool<GpuFlyTextEntity>>();
        }
        if (!flyTextPool.ContainsKey(styleId))
        {
            curPool = new GpuHudEntityPool<GpuFlyTextEntity>(GpuHudEntityType.FlyText, 50, 50, styleId);
            flyTextPool[styleId] = curPool;
            curPool.OnInit(20);
        }
        else curPool = flyTextPool[styleId];
        return curPool?.GetItem();        
    }
    public void RecoverFlyTextEntity(GpuFlyTextEntity entity)
    {
        if(entity != null)
        {
            if (flyTextPool == null || !flyTextPool.ContainsKey(entity.GpuHudAsset.GetStyleId))
            {
                entity.OnDispose();
                return;
            }
            flyTextPool[entity.GpuHudAsset.GetStyleId]?.RecoverItem(entity);
        }
    }
    
    
    
    #endregion

    #region 名字Hud池
    private Dictionary<int, GpuHudEntityPool<GpuPlayerNameEntity>> playerNamePool;
    public GpuPlayerNameEntity GetPlayerNameEntity(int styleId)
    {
        GpuHudEntityPool<GpuPlayerNameEntity> curPool = null;
        if (playerNamePool == null)
        {
            playerNamePool = new Dictionary<int, GpuHudEntityPool<GpuPlayerNameEntity>>();
        }
        if (!playerNamePool.ContainsKey(styleId))
        {
            curPool = new GpuHudEntityPool<GpuPlayerNameEntity>(GpuHudEntityType.PlayerName, 10, 10, styleId);
            playerNamePool[styleId] = curPool;
            curPool.OnInit(5);
        }
        else curPool = playerNamePool[styleId];
        return playerNamePool[styleId].GetItem();
    }
    public void RecoverPlayerNameEntity(GpuPlayerNameEntity entity)
    {
        if (entity != null)
        {
            if (playerNamePool == null || !playerNamePool.ContainsKey(entity.GpuHudAsset.GetStyleId))
            {
                entity.OnDispose();
                return;
            }
            playerNamePool[entity.GpuHudAsset.GetStyleId]?.RecoverItem(entity);
        }
    }
    #endregion

    public void DisposePool()
    {
        if (flyTextPool!=null)
        {
            foreach (var pool in flyTextPool)
            {
                pool.Value.Destroy();
            }
            flyTextPool.Clear();
        }
        if (playerNamePool!=null)
        {
            foreach (var pool in playerNamePool)
            {
                pool.Value.Destroy();
            }
            playerNamePool.Clear();
        }
    }
    
}
